package spitter.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import spitter.exceptions.DuplicateSpittleException;
import spitter.exceptions.SpitterNotFoundException;

@Controller
@RequestMapping("/spittle")
public class SpittleController {
//    @RequestMapping(method = RequestMethod.POST)
//    public String saveSpittle() {
//        try {
//            return "redirect:/spittles";
//        } catch (DuplicateSpittleException e) {
//            return "error/duplicate";
//        }
//    }

    @RequestMapping(value = "save",method = RequestMethod.GET)
    public String saveSpittle() {
        //save spittles.....
        if (true) {
            throw new DuplicateSpittleException();
        }
        return "redirect:/spittles";
    }

    //当抛出DuplicateSpittleExcetion异常的时候，将会委托该方法处理
    @ExceptionHandler(DuplicateSpittleException.class)
    public String handleDuplicateSpittle() {
        return "error/duplicate";
    }
}
